package com.hdw.med.controller;

import com.hdw.common.base.BaseException;
import com.hdw.common.base.BaseResult;
import com.hdw.common.constant.CommonResultConstant;
import com.hdw.common.vo.LayMsg;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.MdMedTbService;
import com.hdw.med.bean.model.MdMedTb;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * mdMedTb控制器
 * Created by hozeData on 2021/07/08.
 */
@RestController
@RequestMapping("/etl/mdMedTb/")
@Api(tags = "元数据-表管理")
@CrossOrigin(origins = "*", maxAge = 3600)
public class MdMedTbController {

    @Autowired
    MdMedTbService mdMedTbService;

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "findByPage", method = RequestMethod.GET)
    public LayMsg findByPage(String filter, Integer page, Integer limit) {
        MdMedTb medTb = new MdMedTb();
        //medTb.setSdTbca("DW");
        //medTb.setFgAct(true);
        PaginationVO<MdMedTb> pageVO = mdMedTbService.findPageByLike(filter, page, limit);
        return LayMsg.success().addCount(pageVO.getTotal()).addData(pageVO.getEntryList());
    }

    @ApiOperation(value = "查询")
    @RequestMapping(value = "find", method = RequestMethod.POST)
    public LayMsg find(@RequestBody MdMedTb mdMedTb) {
        List<MdMedTb> pageVO = mdMedTbService.findByModel(mdMedTb);
        return LayMsg.success().addData(pageVO);
    }

    @ApiOperation(value = "保存")
    @ApiImplicitParam(name = "mdMedPutfd", value = "实体", paramType = "body", required = true, dataType = "MdMedPutfd")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public BaseResult save(@RequestBody MdMedTb mdMedTb, HttpServletRequest request) {
        int num;
        if (StringUtils.isEmpty(mdMedTb.getIdTb())) {
            mdMedTb.setDtSysCre(new Date());
            num = mdMedTbService.insertSelective(mdMedTb);
        } else {
            mdMedTb.setDtSysModi(new Date());
            num = mdMedTbService.updateByIdSelective(mdMedTb);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParam(name = "idTb", value = "主键", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value = "getById", method = RequestMethod.GET)
    public BaseResult getById(Long idTb, HttpServletRequest request) throws BaseException {
        if (StringUtils.isEmpty(idTb)) {
            throw new BaseException("请选择要编辑的项");
        }
        MdMedTb mdMedTb = mdMedTbService.getById(idTb);
        return new BaseResult(CommonResultConstant.SUCCESS, mdMedTb);
    }

    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParam(name = "idTb", value = "主键", paramType = "query", required = true, dataType = "String")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public BaseResult delete(Long idTb, HttpServletRequest request) throws BaseException {
        if (StringUtils.isEmpty(idTb)) {
            throw new BaseException("请选择要删除的项");
        }
        int num = mdMedTbService.deleteById(idTb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }


}
